<?php
namespace App\Http\Controllers\Admin;

use App\Model\AdminUserModel;
use App\Model\AuthAccountModel;
use App\Model\RoleModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class AdminUserController extends Controller
{
    use CallController;

    private $app;

    public function __construct(Request $request)
    {
        parent::__construct();
        $this->app = $this->factory->AdminUser(function ($app) use ($request){
            return $request;
        });
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    private function index()
    {
        if($this->app->ajax()){

            return $this->app->lists();
        }else{

            return view('Admin.AdminUser.index');
        }
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    private function create()
    {
        if($this->app->ajax()){

            $this->app->create();

            return $this->success();
        }else{
            return view('Admin.AdminUser.create');
        }
    }



    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    private function edit($id)
    {
        if($this->app->ajax()){

            $this->app->edit($id);

            return $this->success();
        }else{
            $res = $this->app->first($id);

            return view('Admin.AdminUser.edit' , ['id'=>$id , 'res' => $res]);
        }
    }


    private function changeStatus()
    {
        if($this->app->ajax()){

            $id =  $this->app->input('id');
            $status =  $this->app->input('status');

            $condition = ['id'=>$id];
            $data = ['status'=>$status];

            $this->app->update($condition, $data);

            return $this->success();
        }else{

            throw new \Exception('请求类型错误');
        }
    }



    private function resetPsd()
    {
        if($this->app->ajax()){

            $this->app->resetPsd();

            return $this->success();
        }else{
            throw new \Exception('请求类型错误');
        }
    }



    /**
     * Remove the specified resource from storage.
     *
     * @return \Illuminate\Http\Response
     */
    private function destroy()
    {
        if($this->app->ajax()){

            $this->app->delete();

            return $this->success();
        }else{

            throw new \Exception('请求类型错误');
        }

    }


    private function groupAuth($uid)
    {
        if($this->app->ajax()){

            $this->app->groupAuth($uid);

            return $this->success();
        }else{
            $roleModel = new RoleModel();
            $roles = $roleModel->get();

            $auths = $this->factory->Auth(function ($app){})->classificationAuthArr();
            $accountModel = new AuthAccountModel();
            $res = $accountModel->where(['uid'=>$uid])->first();
            if($res){
               $res = $res->toArray();
            }
            return view('Admin.AdminUser.groupAuth' , ['uid'=>$uid ,'res'=>$res, 'roles' => $roles, 'auths' => $auths]);
        }

    }
}
